﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class KhenthuongDAL : BaseDAL, UTL.IBaseDAL
    {
       #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery ("sp_SelectAll_Khenthuong");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Khenthuong {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

       

        public bool Insert(object obj)
        {
            var o = (Khenthuong)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Khenthuong '{0}', N'{1}', N'{2}', '{3}' ";
            sql = string.Format(sql, o.STT_QDKT, o.NOIDUNG_KT, o.NOIRA_QDKT, o.NGAYRA_QDKT.ToString("dd/MM/yyyy"));
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Khenthuong)obj;
            var sql = "set dateformat dmy; execute sp_Update_Khenthuong '{0}', N'{1}', N'{2}', '{3}' ";
            sql = string.Format(sql, o.STT_QDKT, o.NOIDUNG_KT, o.NOIRA_QDKT, o.NGAYRA_QDKT.ToString("dd/MM/yyyy"));
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select * from tb_Khenthuong";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }       

        public DataTable Search(DAL.Entities.Khenthuong o)
        {
            string sql;
            if (o.STT_QDKT != "")
                sql = @"Select * from tb_Khenthuong Where STT_QDKT like '%" + o.STT_QDKT + "%' ";
            else if (o.NOIDUNG_KT != "")
                sql = @"Select * from tb_Khenthuong Where NOIDUNG_KT like N'%" + o.NOIDUNG_KT + "%' ";
            else if (o.NOIRA_QDKT !="")
                sql = @"Select * from tb_Khenthuong Where NOIRA_QDKT like N'%" + o.NOIRA_QDKT + "%'";
            else
                sql = @"Select * from tb_Khenthuong NGAYRA_QDKT like '%" + o.NGAYRA_QDKT.ToString("dd/MM/yyyy") + "%'";
            return ExecuteQuery(sql);
        }

        #endregion

       #region Other

       public DataTable Select_Max()
       {
           var sql = @"Select Max(STT_QDKT) as Ma From tb_Khenthuong";
           return ExecuteQuery(sql);
       }

       #endregion
    }
}
